package org.malred.v4.server;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * minicat主类
 */
public class Bootstrap {
    /**
     * socket监听的端口号
     */
    private int port=8080;
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * minicat启动需要初始化展开的一些操作
     */
    public void start() throws Exception {
        //加载解析配置文件,web.xml
        loadServlet();
        /*
         * minicat v1.0,http://localhost:8080,返回固定字符串到页面
         */
        ServerSocket serverSocket=new ServerSocket(port);
        System.out.println("minicat start on port: "+port);
        /**
         * Minicat4.0版本
         * 需求: 多线程改造
         */
        while (true){
            Socket socket=serverSocket.accept();
            RequestProcessor requestProcessor =new RequestProcessor(socket,servletMap);
            requestProcessor.start();
        }
    }

    private Map<String,HttpServlet> servletMap=new HashMap<>();
    /**
     * 加载解析web.xml,初始化servlet
     */
    private void loadServlet() throws Exception{
        //加载配置文件
        InputStream resourceAsStream =
                this.getClass().getClassLoader().getResourceAsStream("web.xml");
        SAXReader saxReader=new SAXReader();
        try {
            Document document=saxReader.read(resourceAsStream);
            //获取根元素
            Element rootElement = document.getRootElement();
            //获取servlet标签list
            List<Element> selectNodes = rootElement.selectNodes("//servlet");
            for (int i = 0; i < selectNodes.size(); i++) {
                Element element=selectNodes.get(i);
                //<servlet-name>malguy</servlet-name>
                Element servletNameEle = (Element) element.selectSingleNode("servlet-name");
                String servletName = servletNameEle.getStringValue();
                //<servlet-class>server.MiniServlet</servlet-class>
                Element servletClassEle = (Element) element.selectSingleNode("servlet-class");
                String servletClass = servletClassEle.getStringValue();
                //根据servlet-name找到对应的mapping,找到url-pattern
                Element servletMapping = (Element) rootElement.selectSingleNode
                        ("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
                //url-pattern
                String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
                servletMap.put(urlPattern, (HttpServlet) Class.forName(servletClass).newInstance());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

}